package canglang.scan.core.base.dao;

import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import canglang.scan.core.base.pagination.PaginatedListHelper;

@SuppressWarnings("unchecked")
public class BaseDAO extends SqlMapClientDaoSupport {

    /**
     * Log variable for all child classes. Uses LogFactory.getLog(getClass())
     * from Commons Logging
     */
    protected final Log log = LogFactory.getLog(getClass());
    
    public Object insert(String sql){
        return getSqlMapClientTemplate().insert(sql);
    }    
    
    public Object insert(String sql, HashMap map){
        return getSqlMapClientTemplate().insert(sql,map);
    }

    public List queryForList (String statementName, Object parameterObject, int skipResults, int maxResults)
            throws SQLException {
        return getSqlMapClientTemplate().queryForList(statementName, parameterObject, skipResults, maxResults);
    }

    public List queryForList (String statementName, int skipResults, int maxResults) throws DataAccessException {
        return getSqlMapClientTemplate().queryForList(statementName, skipResults, maxResults);
    }

    public List queryForList (final String statementName, final Object parameterObject) throws DataAccessException {
        return getSqlMapClientTemplate().queryForList(statementName, parameterObject);
    }

    public List queryForList (String statementName) throws DataAccessException {
        return getSqlMapClientTemplate().queryForList(statementName);
    }

    public Object queryForObject (final String statementName, final Object parameterObject, final Object resultObject)
            throws DataAccessException {
        return getSqlMapClientTemplate().queryForObject(statementName, parameterObject, resultObject);
    }

    public Object queryForObject (final String statementName, final Object parameterObject) throws DataAccessException {
        return getSqlMapClientTemplate().queryForObject(statementName, parameterObject);
    }

    public Object queryForObject (String statementName) throws DataAccessException {
        return getSqlMapClientTemplate().queryForObject(statementName);
    }

    protected Map constructPaginationMap (PaginatedListHelper paginaredList, Map map) {
        int fullSize=paginaredList.getFullListSize();
        int currentRange=paginaredList.getObjectsPerPage()*paginaredList.getPageNumber();
        int perPage=paginaredList.getObjectsPerPage();
        if(currentRange>fullSize){
            currentRange=fullSize;
            perPage=currentRange%perPage;
        }
        map.put(PaginatedListHelper.REC_PER_PAGE,perPage );
        map.put(PaginatedListHelper.CURRENT_RANGE,currentRange);
        map.put(PaginatedListHelper.SORT_FIELD, paginaredList.getSortCriterion());
        map.put(PaginatedListHelper.SORT_DIR, paginaredList.getSortDirection());     
        return map;
    }
    

    protected Map objectToMap (Object to) {
        try {
            return PropertyUtils.describe(to);
        } catch (IllegalAccessException e) {
            if (logger.isDebugEnabled()) {
                logger.debug(e.getStackTrace());
            }
        } catch (InvocationTargetException e) {
            if (logger.isDebugEnabled()) {
                logger.debug(e.getStackTrace());
            }
        } catch (NoSuchMethodException e) {
            if (logger.isDebugEnabled()) {
                logger.debug(e.getStackTrace());
            }
        }
        return new HashMap();
    }


}
